#include "XMLDocTest.h"


using namespace std;

	int xml_document_test::load_from_string_test()
	{
		std::ifstream ifs("test.xml");
		std::string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );   //Loads the content of the entire file into a string -'content'*/
		

		xml_document doc;
		
		if(doc.load_from_string(content)!=0) 
			{
				cout<<"Error Loading from string"<<endl; 
				return 1;
			}
		else 
			return 0;
	}

	//tests Loading an XML into XMLDocument object
	int xml_document_test::load_from_file_test()
	{
		xml_document doc;
		if(doc.load_from_file("test.xml")!=0)
		{
			cout<<"Error loading from file"<<endl;
			return 1;
		}
		else 
			return 0;
	}

	//Returns the root node of the XMLDocument
	int xml_document_test::get_root_node_test()
	{
		xml_document doc;							//populate the parse tree
		if(doc.load_from_file("test.xml")!=0)
		{
			cout<<"Error loading from file"<<endl;
			return 1;
		}

		xmlnode *node = doc.get_root_node();		//get the root node in xmlnode *node

		if(node==NULL)								//if parse tree is not populated yet. 
		{
			cout<<"Error Retrieving Root Node or parse tree is empty"<<endl;
			return 1;
		}

		if(node->get_name()!="catalog")				//in test.xml root node is catalog -- hard coding
		{
			cout<<"Error Retrieving Root Node"<<endl;
			return 1;
		}

		if(node->get_parent_node()!=NULL)			//if parent exists then its not the root node
		{
			cout<<"Error Retrieving Root Node"<<endl;
			return 1;
		}
		else 
		return 0;
	}

	// DOM Methods
	int xml_document_test::get_elements_by_tag_name_test()
	{

		xml_document doc;										//populate the parse tree
		if(doc.load_from_file("test.xml")!=0)
		{
			cout<<"Error loading from file"<<endl;
			return 1;
		}

		xmlnodes nodevec;
		nodevec = doc.get_elements_by_tag_name("book");			//hard coded - but can be any string as well

		if(nodevec.size()==0) 
		{
			cout<<"Error getting elements - or no element exists "<<endl; //What is the condition that no element exists with that tag?
			return 1;
		}

		for(int i=0;i<nodevec.size();i++)
			if(nodevec[i]->get_name()!="book")						//Hard coding the tag name for this file
			{
				cout<<"Error getting elements by tag name"<<endl;
				return 1;
			}
			
		return 0;
	}
